package soar.test.xml.jdom;

import java.io.File;
import java.util.List;
import java.util.HashMap;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class Conf
{
    private static final String __CLASS__ = Conf.class.getName();

    public static boolean debug;

    public static class process
    {
        public static class log
        {
            public static String file;
            public static String dir;
            public static boolean debug = false;
        }
    }

    private static final int MYSQL_DEFAULT_PORT = 3306;

    public static class databases
    {
        public static class Database
        {
            public String id;
            public String user;
            public String password;
            public String database;
            public String host;
            public int port;
        }

        public static HashMap<String, Database> database = new HashMap<String, Database>();
    }

    public static boolean init(String conf_file)
    {
        final String __METHOD__ = "init";

        try
        {
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(new File(conf_file));
            Element root = doc.getRootElement();

            System.out.println("Conf:");
            System.out.println("----------------------------------------");

            load_process(root);
            System.out.println("----------------------------------------");

            load_databases(root);
            System.out.println("----------------------------------------");

            System.out.println("========================================");
        }
        catch (Exception e)
        {
            System.err.println(String.format("%s.%s: load %s fail: %s",
                    __CLASS__, __METHOD__, conf_file, e.toString()));
            return false;
        }

        return true;
    }

    private static void load_process(Element root) throws Exception
    {
        final String __METHOD__ = "load_process";

        String level1_name;
        Element level1;
        String level2_name;
        Element level2;
        String name;
        String s;
        level1_name = "process";
        level1 = root.getChild(level1_name);
        if (level1 == null)
        {
            throw new Exception(String.format("%s: %s not found", __METHOD__,
                    level1_name));
        }

        level2_name = "log";
        level2 = level1.getChild(level2_name);
        if (level2 == null)
        {
            throw new Exception(String.format("%s: %s.%s not found",
                    __METHOD__, level1_name, level2_name));
        }

        name = "file";
        s = level2.getChildTextTrim(name);
        if (s == null || s.isEmpty())
        {
            throw new Exception(String.format("%s: %s.%s.%s not found",
                    __METHOD__, level1_name, level2_name, name));
        }
        process.log.file = s;
        System.out.println(String.format("%s.%s.%s: %s", level1_name,
                level2_name, name, s));

        name = "dir";
        s = level2.getChildTextTrim(name);
        if (s == null || s.isEmpty())
        {
            throw new Exception(String.format("%s: %s.%s.%s not found",
                    __METHOD__, level1_name, level2_name, name));
        }
        process.log.dir = s;
        System.out.println(String.format("%s.%s.%s: %s", level1_name,
                level2_name, name, s));

        name = "debug";
        s = level2.getChildTextTrim(name);
        if (s == null || s.isEmpty())
        {
            System.out.println(String.format("%s: %s.%s.%s not found",
                    __METHOD__, level1_name, level2_name, name));
            s = "False";
        }
        process.log.debug = (s.compareToIgnoreCase("True") == 0) ? true : false;
        System.out.println(String.format("%s.%s.%s: %s", level1_name,
                level2_name, name, s));
    }

    private static void load_databases(Element root) throws Exception
    {
        final String __METHOD__ = "load_databases";

        String level1_name;
        Element level1;
        String level2_name;
        List<Element> level2;
        String name;
        String s;
        level1_name = "databases";
        level1 = root.getChild(level1_name);
        if (level1 == null)
        {
            throw new Exception(String.format("%s: %s not found", __METHOD__,
                    level1_name));
        }

        level2_name = "database";
        level2 = level1.getChildren(level2_name);
        if (level2 == null)
        {
            throw new Exception(String.format("%s: %s.%s not found",
                    __METHOD__, level1_name, level2_name));
        }
        int count = 0;
        for(Element level2_item : level2)
        {
            databases.Database node = new databases.Database();

            name = "id";
            s = level2_item.getChildTextTrim(name);
            if (s == null || s.isEmpty())
            {
                throw new Exception(String.format("%s: %s.%s[%d].%s not found",
                        __METHOD__, level1_name, level2_name, count, name));
            }
            node.id = s;
            System.out.println(String.format("%s.%s[%d].%s: %s", level1_name,
                    level2_name, count, name, s));

            name = "user";
            s = level2_item.getChildTextTrim(name);
            if (s == null || s.isEmpty())
            {
                throw new Exception(String.format("%s: %s.%s[%d].%s not found",
                        __METHOD__, level1_name, level2_name, count, name));
            }
            node.user = s;
            System.out.println(String.format("%s.%s[%d].%s: %s", level1_name,
                    level2_name, count, name, s));

            name = "password";
            s = level2_item.getChildTextTrim(name);
            if (s == null || s.isEmpty())
            {
                throw new Exception(String.format("%s: %s.%s[%d].%s not found",
                        __METHOD__, level1_name, level2_name, count, name));
            }
            node.password = s;
            System.out.println(String.format("%s.%s[%d].%s: %s", level1_name,
                    level2_name, count, name, s));

            name = "database";
            s = level2_item.getChildTextTrim(name);
            if (s == null || s.isEmpty())
            {
                throw new Exception(String.format("%s: %s.%s[%d].%s not found",
                        __METHOD__, level1_name, level2_name, count, name));
            }
            node.database = s;
            System.out.println(String.format("%s.%s[%d].%s: %s", level1_name,
                    level2_name, count, name, s));

            name = "host";
            s = level2_item.getChildTextTrim(name);
            if (s == null || s.isEmpty())
            {
                s = "localhost";
            }
            node.host = s;
            System.out.println(String.format("%s.%s[%d].%s: %s", level1_name,
                    level2_name, count, name, s));

            name = "port";
            s = level2_item.getChildTextTrim(name);
            if (s == null || s.isEmpty())
            {
                s = "" + MYSQL_DEFAULT_PORT;
            }
            node.port = Integer.parseInt(s);
            System.out.println(String.format("%s.%s[%d].%s: %s", level1_name,
                    level2_name, count, name, s));

            databases.database.put(node.id, node);
            ++count;
        }
    }
}
